package com.ruoyi.my.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.my.domain.SignIn;
import com.ruoyi.my.service.ISignInService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * signController
 * 
 * @author ruoyi
 * @date 2025-10-24
 */
@RestController
@RequestMapping("/my/sign")
public class SignInController extends BaseController
{
    @Autowired
    private ISignInService signInService;

    /**
     * 查询sign列表
     */
    @PreAuthorize("@ss.hasPermi('my:sign:list')")
    @GetMapping("/list")
    public TableDataInfo list(SignIn signIn)
    {
        startPage();
        List<SignIn> list = signInService.selectSignInList(signIn);
        return getDataTable(list);
    }

    /**
     * 导出sign列表
     */
    @PreAuthorize("@ss.hasPermi('my:sign:export')")
    @Log(title = "sign", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SignIn signIn)
    {
        List<SignIn> list = signInService.selectSignInList(signIn);
        ExcelUtil<SignIn> util = new ExcelUtil<SignIn>(SignIn.class);
        util.exportExcel(response, list, "sign数据");
    }

    /**
     * 获取sign详细信息
     */
    @PreAuthorize("@ss.hasPermi('my:sign:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(signInService.selectSignInById(id));
    }

    /**
     * 新增sign
     */
    @PreAuthorize("@ss.hasPermi('my:sign:add')")
    @Log(title = "sign", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SignIn signIn)
    {
        return toAjax(signInService.insertSignIn(signIn));
    }

    /**
     * 修改sign
     */
    @PreAuthorize("@ss.hasPermi('my:sign:edit')")
    @Log(title = "sign", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SignIn signIn)
    {
        return toAjax(signInService.updateSignIn(signIn));
    }

    /**
     * 删除sign
     */
    @PreAuthorize("@ss.hasPermi('my:sign:remove')")
    @Log(title = "sign", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(signInService.deleteSignInByIds(ids));
    }

    /**
     * 接收扫码结果
     */
    @PostMapping("/scanResult")
    public AjaxResult receiveScanResult(@RequestBody SignIn signIn)
    {
        try {
            // 在控制台醒目地展示扫码结果
            System.out.println("================== 扫码结果 ==================");
            System.out.println("接收时间: " + new java.util.Date());
            System.out.println("扫码数据: " + signIn);
            System.out.println("=============================================");
            
            // 记录到日志
            logger.info("收到扫码结果: {}", signIn);
            
            // 如果需要保存扫码记录，可以调用service
             signInService.insertSignIn(signIn);
            
            return AjaxResult.success("扫码结果接收成功", signIn);
        } catch (Exception e) {
            System.err.println("【错误】接收扫码结果失败: " + e.getMessage());
            logger.error("接收扫码结果失败", e);
            return AjaxResult.error("扫码结果接收失败: " + e.getMessage());
        }
    }
}
